package io.github.wppli.infrastructure.persistent.repository;

import io.github.wppli.domain.authenticate.model.entity.UserEntity;
import io.github.wppli.domain.authenticate.repository.IAuthenticateRepository;
import io.github.wppli.infrastructure.persistent.dao.IUserDao;
import io.github.wppli.infrastructure.persistent.po.User;
import io.github.wppli.types.exception.RegisterFailedException;
import io.github.wppli.types.snow.SnowflakeIdGenerator;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.UUID;

import static io.github.wppli.types.enums.ResponseCode.REGISTER_FAILED_ERROR;

/**
 * @author li--jiaqiang 2024−12−25
 */
@Repository
public class AuthenticateRepository implements IAuthenticateRepository {

    private final IUserDao userDao;
    private final SnowflakeIdGenerator snowflakeIdService;
    public AuthenticateRepository(IUserDao userDao, SnowflakeIdGenerator snowflakeIdService) {
        this.userDao = userDao;
        this.snowflakeIdService = snowflakeIdService;
    }

    @Override
    public void registerWithEmail(String contactInformation) {
        User user = User.builder()
                .id(snowflakeIdService.nextId())
                .email(contactInformation)
                .createdAt(new Date())
                .updatedAt(new Date())
                .socialId(UUID.randomUUID().toString())
                .username(UUID.randomUUID().toString())
                .roleType("ROLE_USER")
                .build();
        int status = userDao.insertOneWithEmail(user);
        if (status != 1) {
            throw new RegisterFailedException(REGISTER_FAILED_ERROR.getCode(), REGISTER_FAILED_ERROR.getInfo());
        }
    }

    @Override
    public UserEntity queryUserInfoWithEmail(String email) {
        User user = userDao.queryOneWithEmail(email);
        // 类型转换
        return getUserEntity(user);

    }

    @Override
    public UserEntity queryUserInfoWithPhoneNumber(String phoneNumber) {
        User user = userDao.queryOneWithPhoneNumber(phoneNumber);
        // 类型转换
        return getUserEntity(user);
    }

    private UserEntity getUserEntity(User user) {
        if (null == user) {
            return null;
        }
        return UserEntity.builder()
                .id(user.getId())
                .username(user.getUsername())
                .password(user.getPassword())
                .email(user.getEmail())
                .phone(user.getPhone())
                .roleType(user.getRoleType())
                .followCount(user.getFollowCount())
                .followerCount(user.getFollowerCount())
                .favoriteCount(user.getFavoriteCount())
                .age(user.getAge())
                .bio(user.getBio())
                .avatarUrl(user.getAvatarUrl())
                .build();
    }

    @Override
    public void setUserPasswordWithEmail(String email, String encodedPassword) {
        userDao.setUserPasswordWithEmail(email, encodedPassword);
    }

    @Override
    public void setUserPasswordWithPhoneNumber(String phoneNumber, String encodedPassword) {
        userDao.setUserPasswordWithPhoneNumber(phoneNumber, encodedPassword);
    }

    @Override
    public void bindEmailWithUsername(String email, String username) {
        userDao.bindEmailWithUsername(email, username);
    }

    @Override
    public UserEntity queryUserInfoById(String userId) {
        User user = userDao.queryUserInfoById(userId);
        return getUserEntity(user);
    }

    @Override
    public UserEntity queryUserInfoByUsername(String username) {
        User user = userDao.queryUserInfoByUsername(username);
        // 类型转换
        if (null == user) {
            return null;
        }
        return getUserEntity(user);
    }

    @Override
    public void queryUserInfo() {

    }

    @Override
    public void registerWithPhoneNumber(String contactInformation) {
        User user = User.builder()
                .id(snowflakeIdService.nextId())
                .phone(contactInformation)
                .createdAt(new Date())
                .updatedAt(new Date())
                .socialId(UUID.randomUUID().toString())
                .username(UUID.randomUUID().toString())
                .roleType("ROLE_USER")
                .build();
        int status = userDao.insertOneWithPhoneNumber(user);
        if (status != 1) {
            throw new RegisterFailedException(REGISTER_FAILED_ERROR.getCode(), REGISTER_FAILED_ERROR.getInfo());
        }
    }
}